﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace FreeDream.Common.Helper
{
    /// <summary>
    /// 分页
    /// </summary>
    public class PageHelper
    {
        #region 分页
        /// <summary>
        /// 得到分页 链接
        /// </summary>
        /// <param name="urlFmt">url 模板</param>
        /// <param name="pageIndex">当前索引</param>
        /// <param name="totalCount">总记录数</param>
        /// <param name="pageSize">页面大小</param>
        /// <param name="num">前后显示数量</param>
        /// <returns></returns>
        public static string GetPagingHTML(string urlFmt, int pageIndex, int totalCount, int pageSize, int num)
        {
            int pageCount = 0;
            if (totalCount % pageSize == 0)
                pageCount = totalCount / pageSize;
            else
                pageCount = totalCount / pageSize + 1;

            int prevIndex = pageIndex - 1;
            int nextIndex = pageIndex + 1;
            string firstPage = "javascript:void(0)";
            string lastPage = "javascript:void(0)";
            string middlePage = "javascript:void(0)";
            if (prevIndex > 0)
            {
                firstPage = string.Format(urlFmt, 1);
                middlePage = string.Format(urlFmt, prevIndex);
            }
            StringBuilder sbPage = new StringBuilder();
            sbPage.AppendFormat("<ul class='pagination'>");

            if (pageIndex <= 1)
            {
                sbPage.AppendFormat("<li class='disabled'><a  href=\"{0}\" title=\"首页\">首&nbsp;页</a></li><li class='disabled'><a  aria-label='Previous' href=\"{1}\"><span aria-hidden='true'>«</span> </a></li>", firstPage, middlePage);
            }
            else
            {
                sbPage.AppendFormat("<li><a aria-label='Previous' href=\"{0}\" title=\"首页\">首&nbsp;页</a></li><li><a  href=\"{1}\"><span aria-hidden='true'>«</span> </a></li>", firstPage, middlePage);
            }

            int allCount = num * 2 + 1;
            int begin = 1;
            int end = 1;
            if (pageCount <= allCount)
                end = pageCount;
            else
            {
                if (pageIndex - num <= 0)
                    end = allCount;
                else if (pageIndex + num > pageCount)
                {
                    begin = pageCount - allCount + 1;
                    end = pageCount;
                }
                else
                {
                    begin = pageIndex - num;
                    end = pageIndex + num;
                }
            }
            for (int i = begin; i <= end; i++)
            {
                middlePage = string.Format(urlFmt, i);
                if (i == pageIndex)
                {
                    sbPage.AppendFormat("<li class='active'><a href='{0}' {2}>{1}</a></li>", middlePage, i, "class='curre'");
                }
                else
                {
                    sbPage.AppendFormat("<li><a href='{0}'>{1}</a></li>", middlePage, i);
                }
            }
            middlePage = "javascript:void(0)";
            if (nextIndex <= pageCount)
            {
                middlePage = string.Format(urlFmt, nextIndex);
                lastPage = string.Format(urlFmt, pageCount);
            }

            if (pageIndex == pageCount || pageCount == 0)
            {
                //sbPage.AppendFormat("<li class='disabled'><a  href='{0}'><span aria-hidden='true'>»</span></a> </li><li class='disabled'><a  href='{1}'>尾页</a> </li><li><span style='border-bottom: 0;border-top: 0;border-right: 0;color:#009688'>共<font style='color:#009688'>{2}</font>页</span></li>", middlePage, lastPage, pageCount);
                sbPage.AppendFormat("<li class='disabled'><a  href='{0}'><span aria-hidden='true'>»</span></a> </li><li class='disabled'><a  href='{1}'>尾页</a> </li>", middlePage, lastPage);
            }
            else
            {
                //sbPage.AppendFormat("<li><a  aria-label='Next' href='{0}'><span aria-hidden='true'>»</span> </a></li><li><a  href='{1}'>尾&nbsp;页</a></li><li><span  style=' border-bottom: 0;border-top: 0;border-right: 0;color:#009688'> 共<font style='color:#009688'>{2}</font>页</span></li>", middlePage, lastPage, pageCount);
                sbPage.AppendFormat("<li><a  aria-label='Next' href='{0}'><span aria-hidden='true'>»</span> </a></li><li><a  href='{1}'>尾&nbsp;页</a></li>", middlePage, lastPage);
            }

            ///sbPage.AppendFormat("<a href='{0}'>下一页</a>", middlePage);
            sbPage.AppendFormat("</ul>");
            return sbPage.ToString();
        }

        /// <summary>
        /// 得到分页 链接
        /// </summary>
        /// <param name="urlFmt">url 模板</param>
        /// <param name="pageIndex">当前索引</param>
        /// <param name="totalCount">总记录数</param>
        /// <param name="pageSize">页面大小</param>
        /// <param name="num">前后显示数量</param>
        /// <param name="IsOnclick">是否是单击查询（true为onclick事件、false为href链接）</param>
        /// <returns></returns>
        public static string GetPagingHTML(string urlFmt, int pageIndex, int totalCount, int pageSize, int num, bool IsOnclick)
        {
            int pageCount = 0;
            if (totalCount % pageSize == 0)
                pageCount = totalCount / pageSize;
            else
                pageCount = totalCount / pageSize + 1;

            int prevIndex = pageIndex - 1;
            int nextIndex = pageIndex + 1;
            string firstPage = "javascript:void(0)";
            string lastPage = "javascript:void(0)";
            string middlePage = "javascript:void(0)";
            if (prevIndex > 0)
            {
                firstPage = string.Format(urlFmt, 1);
                middlePage = string.Format(urlFmt, prevIndex);
            }
            StringBuilder sbPage = new StringBuilder();
            sbPage.AppendFormat("<ul class='pagination'>");

            if (pageIndex <= 1)
            {
                if (IsOnclick)
                {
                    sbPage.AppendFormat("<li class='disabled'><a  onclick=\"{0}\" title=\"首页\">首&nbsp;页</a></li><li class='disabled'><a  aria-label='Previous' onclick=\"{1}\"><span aria-hidden='true'>«</span> </a></li>", firstPage, middlePage);
                }
                else
                {
                    sbPage.AppendFormat("<li class='disabled'><a  href=\"{0}\" title=\"首页\">首&nbsp;页</a></li><li class='disabled'><a  aria-label='Previous' href=\"{1}\"><span aria-hidden='true'>«</span> </a></li>", firstPage, middlePage);
                }

            }
            else
            {
                if (IsOnclick)
                {

                    sbPage.AppendFormat("<li style='cursor: pointer;'><a aria-label='Previous' onclick=\"{0}\" title=\"首页\">首&nbsp;页</a></li><li><a style='cursor: pointer;' onclick=\"{1}\"><span aria-hidden='true'>«</span> </a></li>", firstPage, middlePage);
                }
                else
                {
                    sbPage.AppendFormat("<li><a aria-label='Previous' href=\"{0}\" title=\"首页\">首&nbsp;页</a></li><li><a  href=\"{1}\"><span aria-hidden='true'>«</span> </a></li>", firstPage, middlePage);
                }

            }

            int allCount = num * 2 + 1;
            int begin = 1;
            int end = 1;
            if (pageCount <= allCount)
                end = pageCount;
            else
            {
                if (pageIndex - num <= 0)
                    end = allCount;
                else if (pageIndex + num > pageCount)
                {
                    begin = pageCount - allCount + 1;
                    end = pageCount;
                }
                else
                {
                    begin = pageIndex - num;
                    end = pageIndex + num;
                }
            }
            for (int i = begin; i <= end; i++)
            {
                middlePage = string.Format(urlFmt, i);
                if (i == pageIndex)
                {
                    if (IsOnclick)
                    {
                        sbPage.AppendFormat("<li style='cursor: pointer;' class='active'><a Onclick='{0}' {2}>{1}</a></li>", middlePage, i, "class='curre'");
                    }
                    else
                    {
                        sbPage.AppendFormat("<li class='active'><a href='{0}' {2}>{1}</a></li>", middlePage, i, "class='curre'");
                    }

                }
                else
                {
                    if (IsOnclick)
                    {
                        sbPage.AppendFormat("<li><a style='cursor: pointer;' Onclick='{0}'>{1}</a></li>", middlePage, i);
                    }
                    else
                    {
                        sbPage.AppendFormat("<li><a href='{0}'>{1}</a></li>", middlePage, i);
                    }
                }
            }
            middlePage = "javascript:void(0)";
            if (nextIndex <= pageCount)
            {
                middlePage = string.Format(urlFmt, nextIndex);
                lastPage = string.Format(urlFmt, pageCount);
            }

            if (pageIndex == pageCount || pageCount == 0)
            {
                if (IsOnclick)
                {
                    sbPage.AppendFormat("<li class='disabled'><a  Onclick='{0}'><span aria-hidden='true'>»</span></a> </li><li class='disabled'><a  Onclick='{1}'>尾页</a> </li><li><span style='border-bottom: 0;border-top: 0;border-right: 0;'>共<font style='color:#009688'>{2}</font>页</span></li>", middlePage, lastPage, pageCount);
                }
                else
                {
                    sbPage.AppendFormat("<li class='disabled'><a  href='{0}'><span aria-hidden='true'>»</span></a> </li><li class='disabled'><a  href='{1}'>尾页</a> </li><li><span style='border-bottom: 0;border-top: 0;border-right: 0;'>共<font style='color:#009688'>{2}</font>页</span></li>", middlePage, lastPage, pageCount);
                }

            }
            else
            {
                if (IsOnclick)
                {
                    sbPage.AppendFormat("<li style='cursor: pointer;'><a  aria-label='Next' Onclick='{0}'><span aria-hidden='true'>»</span> </a></li><li style='cursor: pointer;'><a  Onclick='{1}'>尾&nbsp;页</a></li><li><span  style=' border-bottom: 0;border-top: 0;border-right: 0;'> 共<font style='color:#009688'>{2}</font>页</span></li>", middlePage, lastPage, pageCount);
                }
                else
                {
                    sbPage.AppendFormat("<li><a  aria-label='Next' href='{0}'><span aria-hidden='true'>»</span> </a></li><li><a  href='{1}'>尾&nbsp;页</a></li><li><span  style=' border-bottom: 0;border-top: 0;border-right: 0;'> 共<font style='color:#009688'>{2}</font>页</span></li>", middlePage, lastPage, pageCount);
                }

            }
            ///sbPage.AppendFormat("<a href='{0}'>下一页</a>", middlePage);
            sbPage.AppendFormat("</ul>");
            return sbPage.ToString();
        }

        #endregion
    }
}
